<?php
require_once 'MyPdoPublic.class.php';
class TablePublicAdminUser extends MyPdoPublic{
	//普管修改自己的密码
	function _adminUserUpdatePwd($user_id,$user_name,$pwd,$pwd2){
		$sql = "update public_admin_user set pwd = ? where id = ? and name = ? and pwd = ?";
		$res = $this->dml($sql,array(md5($pwd2),$user_id,$user_name,md5($pwd)));
		if($res){
			return 'ok';
		}else{
			return 'err2';
		}
	}




	//超管对普管重置密码
	public function _resetAdminUserPwd($id,$pwd){
		$sql = "update public_admin_user set pwd = md5({$pwd}) where id = ?";
		$res = $this->dml($sql,array($id));
		if($res){
			return 'ok';
		}else{
			return 'err2';
		}
	}



	//普管城市权限的添加或删除
	public function _adminUserPermAdd($userId,$cityCode,$permStat){
		$sql = "select code_list from public_admin_user where id = ?";
		$res = $this->dql($sql,array($userId),1);
		if(!$res["code_list"]){		//初始添加
			$codeList[] = $cityCode;
		}else{
			$codeList = json_decode($res['code_list'],1);
			if($permStat == 2){	//删除
				if(count($codeList) < 1){
					return 'err4';	//没有权限
					exit(json_encode($tmp_arr));
				}
				$codeList2 = array();
				foreach($codeList as $v){
					if(intVal($v) == intVal($cityCode)){
						continue;
					}
					$codeList2[] = $v;
				}
				$codeList = $codeList2;
			}else{				//添加
				if(in_array($cityCode, $codeList)){
					return 'err2';	//已有该城市权限
					exit(json_encode($tmp_arr));
				}
				$codeList[] = $cityCode;
			}
		}

		$sql2 = "update public_admin_user set code_list = ? where id = ?";
		$res2 = $this->dml($sql2,array(json_encode($codeList),$userId));
		if($res2){
			return 'ok';
		}else{
			return 'err3';	//操作失败
		}
	}


	//删除普管
	public function _adminUserDel($id){
		$sql = "delete from public_admin_user where id = ?";
		$res = $this->dml($sql,array($id));
		if($res){
			return 'ok';
		}else{
			return 'err2';
		}
	}


	//添加普管
	public function _adminUserAdd($name,$pwd,$info){
		$sqlc = "select id from public_admin_user where name = ?";
		$resc = $this->dql($sqlc,array($name));
		if($resc){
			return 'err3';	//用户已存在
		}
		$sql = "insert into public_admin_user(name,pwd,remarks,ctime) values(?,?,?,?)";
		$res = $this->dml($sql,array($name,$pwd,$info,date("Y-m-d")));
		if($res){
			return 'ok';
		}else{
			return 'err2';
		}
	}


	//通过id获取普管城市权限列表
	public function _getAdminUserCodeList($id){
		$sql = "select code_list from public_admin_user where id = ?";
		return $this->dql($sql,array($id),1);
	}



	/*
		### 通过账号获取普管信息
	*/
	public function _getAdminUserInfo($user){
		$sql = "select * from public_admin_user where name = ?";
		$res = $this->dql($sql,array($user),1);
		if($res){
			return $res;
		}else{
			return false;
		}
	}


	//获取普管列表
	public function _getAdminUserList(){
		$sql = "select id,name,remarks,ctime from public_admin_user order by id desc limit 10";
		$res = $this->dql($sql,array());
		if($res){
			$data = $res;
		}else{
			$data = array();
		}
		return $data;
	}


}